﻿// ================================================================================
// The contents of this file are subject to the Mozilla Public License
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/2.0/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
// ================================================================================
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace Reflex.Os.Aion.View
{
	/// <summary>
	/// Represents the RadarView.
	/// </summary>
	partial class RadarView : Window
	{
		/// <summary>
		/// Initialize a new instance of the RadarView class.
		/// </summary>
		public RadarView()
		{
			// Initialize the component.
			InitializeComponent();

			// Occurs before closing.
			Closing += ( sender, e ) =>
			{
				// Cancel the event.
				e.Cancel = true;

				// Hide the window.
				Hide();
			};

			// Occurs when the mouse button is pressed.
			Image.MouseDown += ( sender, e ) =>
			{
				// Check if the button associated to the event is the left button.
				if ( e.ChangedButton == MouseButton.Left )
				{
					// Releases the mouse capture from a window in the current thread and restores normal mouse input processing.
					ReleaseCapture();

					// Send a message.
					SendMessage( new WindowInteropHelper( this ).Handle, 0xA1 /*WM_NCLBUTTONDOWN*/, 0x2 /*HT_CAPTION*/, 0);
				}
			};
		}

		#region PInvoke
		/// <summary>
		/// Releases the mouse capture from a window in the current thread and restores normal mouse input processing.
		/// </summary>
		/// <returns></returns>
		[DllImport( "user32.dll" )]
		private static extern bool ReleaseCapture();

		/// <summary>
		/// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
		/// </summary>
		/// <param name="hWnd">A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.</param>
		/// <param name="Msg">The message to be sent.</param>
		/// <param name="wParam">Additional message-specific information.</param>
		/// <param name="lParam">Additional message-specific information.</param>
		[DllImport( "user32.dll" )] private static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, int lParam );
		#endregion
	}
}